<template>
  <!-- 大版心 -->
  <div class="topbar">
    <!-- 版心 -->
    <div class="wrap topbar1">
      <!-- 左边 -->
      <div class="left">欢迎来到叩丁狼积分商城</div>
      <!-- 右边 -->
      <ul class="right">
        <li>
          <img :src="userInfo12.headImg" width="24px" height="24px" alt="" />
        </li>
        <li>用户名：{{ userInfo12.nickName }}</li>
        <li>我的积分:{{ userInfo12.coin }}</li>
        <li>获取积分</li>
        <li>叩丁狼官网</li>
        <li class="btn" @click="changeIsShowLoginModal(true)" v-show="!isLogined">登录</li>
        <li v-show="isLogined" class="cart btn">
          <img src="../assets/img/cart.png" alt="" />
          <span>购物车</span>
          <b>{{ cartTotal }}</b>
        </li>
      </ul>
    </div>
  </div>
</template>
 
<script>
import { mapActions, mapMutations, mapState } from 'vuex'
import { WeixinLoginApi } from '@/request/api'
export default {
  data() {
    return {}
  },
  computed: {
    ...mapState({
      isLogined: (state) => state.LoginStatus.isLogined,
      cartTotal: (state) => state.userInfo1.cartTotal,
      userInfo12: (state) => state.userInfo1.userInfo12
    })
  },
  methods: {
    ...mapMutations({
      changeIsShowLoginModal: 'showModal/changeIsShowLoginModal',
      changeIsLogined: 'LoginStatus/changeIsLogined',
      initUserInfo: 'userInfo1/initUserInfo'
    }),
    ...mapActions({
      asyncchangeIsShowToast: 'showToast/asyncchangeIsShowToast',
      asyncChangeUserInfo: 'userInfo1/asyncChangeUserInfo'
    })
  },
  //监听路由的变化   如果发生改变，当前路由就会得到token值   再根据有没有路由判断当前的登录状态是否改变
  // watch: {
  //   '$route.path': {
  //     handler(newVal, oldVal) {
  //       if (newVal != oldVal) {
  //         let token = localStorage.getItem('v-auth-token')
  //         this.changeIsLogined(Boolean(token))
  //       }
  //     }
  //   }
  // },
  async created() {
    console.log(this.$route.query.code)
    let code = this.$route.query.code
    /* 判断当前路由是否有绑定微信  有就会得到微信里面的code值 */
    if (code) {
      let res = await WeixinLoginApi({
        code
      })
      console.log('微信登录后res:', res)
      if (res.data.code == 0) {
        this.asyncchangeIsShowToast({
          isshow: true,
          msg: '登录成功',
          type: 'success'
        })
        localStorage.setItem('x-auth-token', res.data['x-auth-token'])
        this.changeIsLogined(true)
        /* 清除url上code */
        this.$route.query.code && this.$router.push(this.$route.path)
        this.asyncChangeUserInfo()
      } else if (res.code == 400) {
        this.asyncchangeIsShowToast({
          isshow: true,
          msg: '请重新扫码',
          type: 'success'
        })
      } else if (res.data.code == 407) {
        this.asyncchangeIsShowToast({
          isshow: true,
          msg: '请先绑定手机号登录',
          type: 'success'
        })
        this.changeIsShowLoginModal(true)
        localStorage.setItem('uuid', res.data.uuid)
        console.log(res.data.uuid)
      }
    } else {
      //判断是否有token
      let token = localStorage.getItem('x-auth-token')
      /* Topbar登录按钮的状态 */
      this.changeIsLogined(Boolean(token))
      if (token) {
        this.asyncChangeUserInfo()
      } else {
        this.initUserInfo()
      }
      /*    if (token) {
        this.changeIsLogined(true)
      } else {
        this.changeIsLogined(false)
      } */
    }
  }
  // watch: {
  //   '$route.path': {
  //     handler(newVal, oldVal) {
  //       if (newVal != oldVal) {
  //         let token = localStorage.getItem('x-auth-token')
  //         if (token) {
  //           this.changeIsLogined(true)
  //         } else {
  //           this.changeIsLogined(false)
  //         }
  //       }
  //     }
  //   }
  // }
}
</script>
 
<style lang = "less" scoped>
.topbar {
  width: 1920px;
  height: 40px;
  background: #242b39;
  color: #fff;
  line-height: 40px;
  .topbar1 {
    display: flex;
    justify-content: space-between;
    .right {
      display: flex;
      justify-content: space-between;
      li {
        padding: 0px 10px;
        margin-left: 20px;
        cursor: pointer;
      }
      .btn {
        width: 124px;
        height: 40px;
        background: #0a328e;
        text-align: center;
        b {
          width: 22px;
          height: 22px;
          background: #fd604d;
          border-radius: 50%;
          line-height: 22px;
          margin-left: 4px;
          display: inline-block;
        }
      }
      li:hover {
        color: red;
      }
    }
  }
}
</style>